<template>
  <div class="employee-container">
    <el-row class="employee-header">
      <el-card>
        <el-form label-width="auto" :model="form">
          <el-row type="flex" justify="space-between">
            <el-form-item label="员工账号">
              <el-col :span="8">
                <el-input
                  v-model="form.employeeAccount"
                  class="inputStyle"
                  placeholder="请输入员工账号"
                />
              </el-col>
            </el-form-item>
            <el-form-item label="员工姓名">
              <el-col :span="8">
                <el-input
                  v-model="form.name"
                  class="inputStyle"
                  placeholder="请输入员工姓名"
                />
              </el-col>
            </el-form-item>
            <el-form-item label="工作模式">
              <el-col :span="8">
                <el-select
                  v-model="form.workPatternId"
                  class="inputStyle"
                  placeholder="请输入工作模式"
                >
                  <el-option
                    v-for="item in patterns"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-col>
            </el-form-item>
          </el-row>
          <el-row type="flex">
            <el-form-item style="margin: 0" label="月份">
              <el-date-picker
                v-model="form.month"
                type="month"
                class="inputStyle"
                placeholder="请选择月份"
              />
            </el-form-item>
            <el-form-item label="所属机构" style="padding-left: 28px">
              <tree-data ref="treeData" v-model="form.agencyId" />
            </el-form-item>
            <el-form-item style="margin: 0">
              <el-button
                class="buttonStyle"
                type="primary"
                size="small"
                @click="searchClick"
              >查询</el-button>
              <el-button
                class="buttonStyle"
                size="small"
                @click="resetClick"
              >重置</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-card>
    </el-row>
    <el-row class="employee-footer">
      <el-card class="footer-card">
        <el-row>
          <el-button
            class="footer-button"
            size="small"
            type="primary"
            @click="scheduleClick"
          >排班设置</el-button>
          <el-button size="small" type="success" plain>绑定排班</el-button>
        </el-row>
        <el-row>
          <el-table :data="tableData" stripe style="width: 100%" border>
            <el-table-column prop="" width="50" fixed="left" align="center">
              <template slot="header" slot-scope="scope">
                <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="checkAllChange" />
              </template>
              <template @default="{row}">
                <el-checkbox :checked="checkedCities" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="employeeNumber"
              label="员工账号"
              width="115.7"
              fixed="left"
            />
            <el-table-column
              align="center"
              prop="name"
              label="员工姓名"
              width="115.7"
              fixed="left"
            />
            <el-table-column
              align="center"
              prop="phone"
              label="员工电话"
              width="115.7"
              fixed="left"
            />
            <el-table-column
              align="center"
              prop="userType"
              label="员工角色"
              width="115.7"
              fixed="left"
            />
            <el-table-column
              align="center"
              prop="workPatternId"
              label="当前工作模式"
              width="115.7"
              fixed="left"
            >
              <template #default="{ row }">
                <span>{{ row.workPatternName }}</span>
              </template>
            </el-table-column>
            <el-table-column
              v-for="item in 30"
              :key="item"
              :label="item + '日'"
              prop="workSchedules"
              align="center"
              width="69.7"
            >
              <template #default="{ row }">
                <span
                  :class="{
                    work: row.workSchedules[item],
                    rest: !row.workSchedules[item],
                  }"
                >{{ row.workSchedules[item] ? "上" : "休" }}</span>
              </template>
            </el-table-column>
            <template slot="empty">
              <el-empty :image-size="150" description="暂无数据" />
            </template>
          </el-table>
        </el-row>
        <el-row
          type="flex"
          justify="center"
          style="margin-top: 10px; height: 40px"
          align="middle"
        >
          <el-pagination
            :current-page="form.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-count="pages"
            :page-size="form.pageSize"
            :pager-count="7"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="sizeChange"
            @current-change="gurrentChange"
          />
        </el-row>
      </el-card>
    </el-row>
  </div>
</template>

<script>
import { getScheduleAPI, getPatternsAPI } from '@/api/employee'
import TreeData from '@/components/TreeData/index'
export default {
  name: 'Scheduling',
  components: {
    TreeData
  },
  data() {
    return {
      form: {
        agencyId: '',
        employee: '',
        month: '',
        name: '',
        page: 1,
        pageSize: 10,
        userType: '',
        workPatternId: ''
      },
      // 表格数据
      tableData: [],
      // 工作模式列表
      patterns: [],
      isIndeterminate: true,
      checkAll: false,
      checkedCities: [],
      timer: null,
      pages: 1,
      total: 0
    }
  },
  created() {
    this.schedulings()
    this.getPatterns()
  },
  methods: {
    async schedulings() {
      const { data } = await getScheduleAPI(this.form)
      const itemData = data.data
      this.tableData = itemData.items
      this.total = +itemData.total
      this.pages = +itemData.pages
      // console.log(this.tableData)
      // this.tableData.forEach(item => {
      //   this.checkedCities.push(item.userId)
      // })
    },
    // 获取工作模式
    async getPatterns() {
      const { data } = await getPatternsAPI()
      this.patterns = data.data[1]
    },
    // 选择工作模式
    scheduleClick() {
      this.$router.push({
        path: '/employee/schedulingList'
      })
    },
    // 查询
    searchClick() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.schedulings()
      }, 500)
    },
    // 重置
    resetClick() {
      this.form.agencyId = null
      this.form.name = null
      this.form.employee = null
      this.form.month = null
      this.form.workPatternId = null
      this.$refs.treeData.backups.agentName = null
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.schedulings()
      }, 500)
    },
    checkAllChange(val) {
      if (val) {
        this.checkedCities = this.tableData.map(item => item.userId)
        this.isIndeterminate = false
      }
      console.log(val)
    },
    checkedCitiesChange(val) {
      this.checkedCities = val
      // this.isIndeterminate = false
      console.log(val)
    },

    // 当前页改变
    gurrentChange(page) {
      this.form.page = page
      this.schedulings()
    },
    // 每页条数改变
    sizeChange(pageSize) {
      this.form.pageSize = pageSize
      this.schedulings()
    }
  }
}
</script>

<style scoped lang="scss">
.employee-container {
  padding: 0 20px;

  .employee-header {
    font-size: 13px;
    margin-bottom: 20px;
    .el-form-item {
      padding: 0 20px;
      .inputStyle {
        width: 300px;
      }
    }
    .header-footer {
      margin: 0;
    }
  }
  .buttonStyle {
    width: 80px;
    height: 30px;

    &.buttonStyle:first-child {
      background-color: #189450;
      border-color: #189450;
      margin-right: 10px;

      &:hover {
        opacity: 0.7;
      }
    }
  }
  .employee-footer {
    .footer-card {
      padding: 10px;
      padding-bottom: 0;

      .footer-button {
        margin-bottom: 20px;
        background-color: #189450;
        border-color: #189450;
      }
    }
  }
}
.work {
  display: inline-block;
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.rest {
  display: inline-block;
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}
::v-deep .el-form-item__label {
  font-size: 13px;
}

::v-deep .el-table thead tr th {
  font-size: 13px;
  font-weight: bold;
  background: #f8faff !important;
  border: none;
}
::v-deep .el-table tbody tr td {
  font-size: 13px;
  color: #606266;
  border: none;
}
::v-deep .el-input__inner {
  height: 35px;
  font-size: 13px;
}
::v-deep .el-pagination .el-select .el-input .el-input__inner {
  height: 28px;
  font-size: 13px;
}
</style>
